<?php
/*
 * template/edit.phtml
 */
?>
<?php

use RH\Helper\DateTime;
?>
<?php
$templates	 = $this->templates;
$template	 = $this->template;
$employes	 = $this->employes;
$typeList	 = $this->typeList;
$codes		 = array();
$codec		 = array();
foreach ($this->codes as $code) {
    $codec[$code->id]	 = $code->code;
    $codes[]		 = $code;
}
//var_dump($codes);
?>
<div class='content-body'>
    <?php if ((int) $this->nohead != 1): ?>
        <div class="content-head">
    	<form action="#" method="post" id="planning-list-edit">
    	    <label style='padding: auto 5px; display: inline-block; height: 2.4em; margin-top: 5px'>
    		Libellé du Planning Type : <input type='text' name='pl_libelle' value='<?php echo $template->libelle ?>' size="100" style='border: solid 1px #f63700; width: 50%'>
    	    </label>
    	    <input type="hidden" name="list_id" value="<?php echo $template->id ?>">
    	    <input type="hidden" name="pl_num" value="<?php echo $template->template ?>">
    	    <input type="hidden" name="site_id" value="<?php echo $template->site_id ?>">
    	    <input type="hidden" name="pl_creation" value="<?php echo $template->creation ?>">
    	    <input type="hidden" name="pl_edition" value="<?php echo date('Y-m-d') ?>">
    	</form>
    	<div class='toolbar'>
    	    <button type='button' class='btn btn-success' onclick='return submitAndClose();'>Renommer</button>
		<?php if ((int) $this->fullscreen == 1): ?>
		    <button type="button" class="btn btn-inverse" onclick="normalScreenLayout();">
			Retour à la liste des Semaines
		    </button>
		<?php else: ?>
		    <button type='button' class='btn btn-inverse' onclick='$("#pl-dialog").dialog("close");
			    return false;'>Fermer</button>
			<?php endif; ?>
    	</div>
        </div>
    <?php endif; ?>

    <?php if ((int) $this->fullscreen == 1): ?>
        <button type="button" class="btn btn-info rhx-goback" onclick="normalScreenLayout();">
    	Retour à la liste des Semaines
        </button>
    <?php endif ?>

    <div class='list-container' id="planning-table">
	<table class="pl-table" style='width: 100%'>
	    <thead>
		<tr>
		    <td style='text-align: center' rowspan='2'>
			<span>Mat.</span>
		    </td>
		    <td style='text-align: center' rowspan='2'>
			<span>Poste</span>
		    </td>
		    <td style='text-align: center' rowspan='2'>
			<span>Nom</span>
		    </td>
		    <td style='text-align: center' colspan='3'>
			Lundi
		    </td>
		    <td style='text-align: center' colspan='3'>
			Mardi
		    </td>
		    <td style='text-align: center' colspan='3'>
			Mercredi
		    </td>
		    <td style='text-align: center' colspan='3'>
			Jeudi
		    </td>
		    <td style='text-align: center' colspan='3'>
			Vendredi
		    </td>
		    <td style='text-align: center' colspan='3'>
			Samedi
		    </td>
		    <td style='text-align: center' colspan='3'>
			Dimanche
		    </td>
		</tr>
		<tr style='font-size: 0.7em'>

		    <td>Code</td>
		    <td>Entrée</td>
		    <td>Sortie</td>

		    <td>Code</td>
		    <td>Entrée</td>
		    <td>Sortie</td>

		    <td>Code</td>
		    <td>Entrée</td>
		    <td>Sortie</td>

		    <td>Code</td>
		    <td>Entrée</td>
		    <td>Sortie</td>

		    <td>Code</td>
		    <td>Entrée</td>
		    <td>Sortie</td>

		    <td>Code</td>
		    <td>Entrée</td>
		    <td>Sortie</td>

		    <td>Code</td>
		    <td>Entrée</td>
		    <td>Sortie</td>

		</tr>
	    </thead>

	    <tbody style="font-size: 0.9em; text-align: center; width: 100%">

		<?php $jaune = 1 ?>
		<?php foreach ($templates as $tpl) : ?>
		    <?php $employe = isset($employes[$tpl->matricule]) ? $employes[$tpl->matricule] : new \RHX\Model\Object\Employe($this->adapter); ?>
		    <?php $poste	 = $employe->getPoste(); ?>
    		<tr>
    		    <td class='<?php echo (($jaune % 2) == 0) ? 'color-jaune2' : 'color-jaune1' ?>'>
			    <?php echo $employe->matricule ?>
    		    </td>
    		    <td class='<?php echo (($jaune % 2) == 0) ? 'color-jaune2' : 'color-jaune1' ?>'>
			    <?php echo $poste->code; ?>
    		    </td>
    		    <td class='<?php echo (($jaune % 2) == 0) ? 'color-jaune2' : 'color-jaune1' ?>'>
			    <?php echo $employe->prenom ?>
    		    </td>

    		    <!-- Lundi -->
    		    <td class='color-<?php echo $codec[$tpl->lundi_code] ?>' id='tpl-<?php echo $tpl->id ?>-lundi' style='position: relative;'>
    			<a href='#' class='full-link pl-day-edit code_activite' data-prefix='lundi'  data-pk='<?php echo $tpl->id ?>' data-matricule="<?php echo $employe->matricule; ?>">
				<?php echo $codec[$tpl->lundi_code] ?>
    			</a>
    		    </td>
    		    <td class='color-<?php echo $codec[$tpl->lundi_code] ?>' id="tpl-<?php echo $tpl->id . '-lundi-entree' ?>">
    			<a href='#' class='full-link pl-day-edit entree' data-prefix='lundi'  data-pk='<?php echo $tpl->id ?>' data-matricule="<?php echo $employe->matricule; ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->lundi_entree) ?>
    			</a>
    		    </td>
    		    <td class='color-<?php echo $codec[$tpl->lundi_code] ?>' id="tpl-<?php echo $tpl->id . '-lundi-sortie' ?>">
    			<a href='#' class="full-link pl-day-edit sortie" data-prefix='lundi'  data-pk='<?php echo $tpl->id ?>' data-matricule="<?php echo $employe->matricule; ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->lundi_sortie) ?>
    			</a>
    		    </td>

    		    <!-- Mardi -->
    		    <td class='color-<?php echo $codec[$tpl->mardi_code] ?>' id='tpl-<?php echo $tpl->id ?>-mardi' style='position: relative;'>
    			<a href='#' class="full-link pl-day-edit code_activite" data-prefix='mardi'  data-pk='<?php echo $tpl->id ?>' data-matricule="<?php echo $employe->matricule; ?>">
				<?php echo $codec[$tpl->mardi_code] ?>
    			</a>
    		    </td>
    		    <td class='color-<?php echo $codec[$tpl->mardi_code] ?>' id="tpl-<?php echo $tpl->id . '-mardi-entree' ?>">
    			<a href='#' class="full-link pl-day-edit entree" data-prefix='mardi'  data-pk='<?php echo $tpl->id ?>' data-matricule="<?php echo $employe->matricule; ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->mardi_entree) ?>
    			</a>
    		    </td>
    		    <td class='color-<?php echo $codec[$tpl->mardi_code] ?>' id="tpl-<?php echo $tpl->id . '-mardi-sortie' ?>">
    			<a href='#' class="full-link pl-day-edit sortie" data-prefix='mardi'  data-pk='<?php echo $tpl->id ?>' data-matricule="<?php echo $employe->matricule; ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->mardi_sortie) ?>
    			</a>
    		    </td>

    		    <!-- Mercredi -->
    		    <td class='color-<?php echo $codec[$tpl->mercredi_code] ?>' id='tpl-<?php echo $tpl->id ?>-mercredi' style='position: relative;'>
    			<a href='#' class="full-link pl-day-edit code_activite" data-prefix='mercredi'  data-pk='<?php echo $tpl->id ?>' data-matricule="<?php echo $employe->matricule; ?>">
				<?php echo $codec[$tpl->mercredi_code] ?>
    			</a>
    		    </td>
    		    <td class='color-<?php echo $codec[$tpl->mercredi_code] ?>' id="tpl-<?php echo $tpl->id . '-mercredi-entree' ?>">
    			<a href='#' class="full-link pl-day-edit entree" data-prefix='mercredi'  data-pk='<?php echo $tpl->id ?>' data-matricule="<?php echo $employe->matricule; ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->mercredi_entree) ?>
    			</a>
    		    </td>
    		    <td class='color-<?php echo $codec[$tpl->mercredi_code] ?>' id="tpl-<?php echo $tpl->id . '-mercredi-sortie' ?>">
    			<a href='#' class="full-link pl-day-edit sortie" data-prefix='mercredi'  data-pk='<?php echo $tpl->id ?>' data-matricule="<?php echo $employe->matricule; ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->mercredi_sortie) ?>
    			</a>
    		    </td>

    		    <!-- Jeudi -->
    		    <td class='color-<?php echo $codec[$tpl->jeudi_code] ?>' id='tpl-<?php echo $tpl->id ?>-jeudi' style='position: relative;'>
    			<a href='#' class="full-link pl-day-edit code_activite" data-prefix='jeudi'  data-pk='<?php echo $tpl->id ?>' data-matricule="<?php echo $employe->matricule; ?>">
				<?php echo $codec[$tpl->jeudi_code] ?>
    			</a>
    		    </td>
    		    <td class='color-<?php echo $codec[$tpl->jeudi_code] ?>' id="tpl-<?php echo $tpl->id . '-jeudi-entree' ?>">
    			<a href='#' class="full-link pl-day-edit entree" data-prefix='jeudi'  data-pk='<?php echo $tpl->id ?>' data-matricule="<?php echo $employe->matricule; ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->jeudi_entree) ?>
    			</a>
    		    </td>
    		    <td class='color-<?php echo $codec[$tpl->jeudi_code] ?>' id="tpl-<?php echo $tpl->id . '-jeudi-sortie' ?>">
    			<a href='#' class="full-link pl-day-edit sortie" data-prefix='jeudi'  data-pk='<?php echo $tpl->id ?>' data-matricule="<?php echo $employe->matricule; ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->jeudi_sortie) ?>
    			</a>
    		    </td>

    		    <!-- Vendredi -->
    		    <td class='color-<?php echo $codec[$tpl->vendredi_code] ?>' id='tpl-<?php echo $tpl->id ?>-vendredi' style='position: relative;'>
    			<a href='#' class="full-link pl-day-edit code_activite" data-prefix='vendredi'  data-pk='<?php echo $tpl->id ?>' data-matricule="<?php echo $employe->matricule; ?>">
				<?php echo $codec[$tpl->vendredi_code] ?>
    			</a>
    		    </td>
    		    <td class='color-<?php echo $codec[$tpl->vendredi_code] ?>' id="tpl-<?php echo $tpl->id . '-vendredi-entree' ?>">
    			<a href='#' class="full-link pl-day-edit entree" data-prefix='vendredi'  data-pk='<?php echo $tpl->id ?>' data-matricule="<?php echo $employe->matricule; ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->vendredi_entree) ?>
    			</a>
    		    </td>
    		    <td class='color-<?php echo $codec[$tpl->vendredi_code] ?>' id="tpl-<?php echo $tpl->id . '-vendredi-sortie' ?>">
    			<a href='#' class="full-link pl-day-edit sortie" data-prefix='vendredi'  data-pk='<?php echo $tpl->id ?>' data-matricule="<?php echo $employe->matricule; ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->vendredi_sortie) ?>
    			</a>
    		    </td>

    		    <!-- Samedi -->
    		    <td class='color-<?php echo $codec[$tpl->samedi_code] ?>' id='tpl-<?php echo $tpl->id ?>-samedi' style='position: relative;'>
    			<a href='#' class="full-link pl-day-edit code_activite" data-prefix='samedi'  data-pk='<?php echo $tpl->id ?>' data-matricule="<?php echo $employe->matricule; ?>">
				<?php echo $codec[$tpl->samedi_code] ?>
    			</a>
    		    </td>
    		    <td class='color-<?php echo $codec[$tpl->samedi_code] ?>' id="tpl-<?php echo $tpl->id . '-samedi-entree' ?>">
    			<a href='#' class="full-link pl-day-edit entree" data-prefix='samedi'  data-pk='<?php echo $tpl->id ?>' data-matricule="<?php echo $employe->matricule; ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->samedi_entree) ?>
    			</a>
    		    </td>
    		    <td class='color-<?php echo $codec[$tpl->samedi_code] ?>' id="tpl-<?php echo $tpl->id . '-samedi-sortie' ?>">
    			<a href='#' class="full-link pl-day-edit sortie" data-prefix='samedi'  data-pk='<?php echo $tpl->id ?>' data-matricule="<?php echo $employe->matricule; ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->samedi_sortie) ?>
    			</a>
    		    </td>

    		    <!-- Dimanche -->
    		    <td class='color-<?php echo $codec[$tpl->dimanche_code] ?>' id='tpl-<?php echo $tpl->id ?>-dimanche' style='position: relative;'>
    			<a href='#' class="full-link pl-day-edit code_activite" data-prefix='dimanche'  data-pk='<?php echo $tpl->id ?>' data-matricule="<?php echo $employe->matricule; ?>">
				<?php echo $codec[$tpl->dimanche_code] ?>
    			</a>
    		    </td>
    		    <td class='color-<?php echo $codec[$tpl->dimanche_code] ?>' id="tpl-<?php echo $tpl->id . '-dimanche-entree' ?>">
    			<a href='#' class="full-link pl-day-edit entree" data-prefix='dimanche'  data-pk='<?php echo $tpl->id ?>' data-matricule="<?php echo $employe->matricule; ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->dimanche_entree) ?>
    			</a>
    		    </td>
    		    <td class='color-<?php echo $codec[$tpl->dimanche_code] ?>' id="tpl-<?php echo $tpl->id . '-dimanche-sortie' ?>">
    			<a href='#' class="full-link pl-day-edit sortie" data-prefix='dimanche'  data-pk='<?php echo $tpl->id ?>' data-matricule="<?php echo $employe->matricule; ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->dimanche_sortie) ?>
    			</a>
    		    </td>

    		</tr>
		    <?php $jaune++ ?>
		<?php endforeach ?>

	    </tbody>
	</table>
    </div>
</div>

<script type="text/javascript">
		// Initialisation des liens d'édition
		var openTemplate = '';
		$(document).ready(function() {
<?php if ((int) $this->nohead != 1): ?>
    		    // pl-day-edit
    		    $('.pl-day-edit').click(function(event) {
    			event.preventDefault();

    			// Récupération des variables
    			matricule = $(this).attr('data-matricule');
    			prefix = $(this).attr('data-prefix');
    			pk = $(this).attr('data-pk');
    			id = pk;
    			// Position de la souris
    			x = event.pageX;
    			y = event.pageY;
    			// Taille de l'écran
    			sw = $(window).width();
    			sh = $(window).height();
    			// Taille du dialogue
    			dw = 500;
    			dh = 400;
    			// Positionnement du dialogue
    			px = 0;
    			py = 0;
    			// Test sur la largeur
    			if ((sw - (dw + x)) > 0) {
    			    px = x;
    			} else {
    			    px = x - dw;
    			}
    			// Test sur la hauteur
    			//alert('Hauteur Fenetre : ' + sh + ', Y : ' + y + ', Hauteur Document : ' + dh);
    			if ((sh - y) >= dh - 250) {
    			    py = y - 40;
    			} else {
    			    py = y - dh + 80;
    			}
    			// Suppression des autres DIV conteneur
    			$('.rhPopover').remove();
    			// Ajout d'un div conteneur
    			$('#tpl-' + id + '-' + prefix).append("<div class='rhPopover'></div>");
    			// Chargement du contenu
    			ajax = $.ajax({
    			    type: 'post',
    			    url: '<?php echo $this->url('RHX/default', array('controller'	 => 'template', 'action'	 => 'popin')) ?>',
    			    data: "&id=" + id + "&prefix=" + prefix + '&matricule=' + matricule
    			});
    			ajax.success(function(data) {
    			    $('.rhPopover').hide().html(data).css({
    				'top': py,
    				'left': px
    			    }).show('normal');
    			});
    			ajax.error(function() {
    			    alert("Erreur de chargement");
    			});
    		    });

    		    // Détecter clic en dehors du DIV
    		    $(document).mouseup(function(e) {
    			var container = $(".rhPopover");
    			if (container.has(e.target).length === 0) {
    			    // Suppression du DIV
    			    container.remove();
    			}
    		    });
<?php else: ?>
    		    $('.pl-day-edit').click(function(Event) {
    			Event.preventDefault();
    			$(this).prop('title', 'Aucune Modification n\'est possible');
    		    });
<?php endif; ?>
		});
		function submitAndClose() {
		    ajax = $.ajax({
			type: 'post',
			url: "<?php echo $this->url('RHX/default', array('controller'	 => 'template', 'action'	 => 'savetolist')) ?>",
			data: $('#planning-list-edit').serialize()
		    });
		    ajax.success(function(data) {
			location.reload();
			$("#pl-dialog").dialog("close");
			// alert("OK");
		    });
		    ajax.error(function() {
			alert("Erreur! La Mise à jour a échoué");
		    });
		    return false;
		}
		function justSubmit() {
		    ajax = $.ajax({
			type: 'post',
			url: "<?php echo $this->url('RHX/default', array('controller'	 => 'template', 'action'	 => 'savetolist')) ?>",
			data: $('#planning-list-edit').serialize()
		    });
		    ajax.success(function(data) {
			//
		    });
		    ajax.error(function() {
			alert("Erreur! La Mise à jour a échoué");
		    });
		    return false;
		}
</script>

<style type="text/css">
    /*pl-table{
        border-collapse: collapse;
    }
    .pl-table tr,
    .pl-table td{
        border: 1px solid #DFDFDF;
        padding: 4px 2px;
    }
    .pl-table thead tr{
        background: #95B3D7;
    }
    .pl-table thead td{
        text-align: center;
        min-height: 1.5em;
    }
    .pl-table tbody tr:nth-child(even){
        background: #f8f8f8;
    }*/
    .color-jaune1{
        background: #fff67f;
    }
    .color-jaune2{
        background: #ffee05;
    }
    .color-jaune3{
        background: #ffe84c;
    }
    <?php foreach ($codes as $code): ?>
        .color-<?php echo $code->code ?>{ background: <?php echo $code->couleur; ?>}
    <?php endforeach ?>
    <?php if ((int) $this->nohead == 1): ?>
        .pl-day-edit{
    	cursor: not-allowed;
        }
    <?php endif; ?>
    .rhx-goback{
	position: absolute;
	top: 6px;
	right: 100px;
	z-index: 9999;
    }
    .rhPopover{
	position: absolute;
	z-index: 9999;
	width: 500px;
    }
</style>